package code1.binary_tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * 原题链接：https://leetcode.cn/problems/maximum-depth-of-binary-tree/
 */
public class P104 {

    //层序遍历
    public int maxDepth(TreeNode root) {
        int depth=0;
        Queue<TreeNode> queue=new LinkedList<>();
        if(root==null){
            return depth;
        }
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            depth++;
            for(int i=0;i<size;i++){
                TreeNode node = queue.poll();
                if(node.left!=null){
                    queue.offer(node.left);
                }
                if(node.right!=null){
                    queue.offer(node.right);
                }
            }
        }
        return depth;
    }

    //获取子树的深度
    public int getDepth(TreeNode root){
        if(root==null){
            return 0;
        }
        int depth1 = getDepth(root.left);
        int depth2 = getDepth(root.right);
        int depth=Math.max(depth1,depth2)+1;
        return depth;
    }

    //递归法
    public int maxDepth2(TreeNode root) {
        if(root==null){
            return 0;
        }
        return getDepth(root);
    }

}
